﻿function LTrim(str)
{  
    var i;  
    for(i=0;i<str.length;i++)  
    {  
    if(str.charAt(i)!=" "&&str.charAt(i)!=" ")
    	break;  
    }  
    str=str.substring(i,str.length);  
    return str;  
}  
function RTrim(str)
{  
    var i;  
    for(i=str.length-1;i>=0;i--)  
    {  
    if(str.charAt(i)!=" "&&str.charAt(i)!=" ")
    	break;  
    }  
    str=str.substring(0,i+1);  
    return str;  
}  
function Trim(str)
{  
   var trimStr=LTrim(RTrim(str));
   if(trimStr==" ")
      trimStr="";
    return trimStr;
} 



function $(hw_){return document.getElementById(hw_);}

function Move(evn,o){
	_bW=document.body.clientWidth;
	_left1=document.body.scrollLeft+evn.clientX+10;
	_oW=o.offsetWidth;
	_left=((evn.clientX+_oW)>_bW)?(_left1-_oW-10):_left1;
	if((evn.clientX+_oW)>_bW){_left=(_oW<evn.clientX)?(_left1-_oW-10):_left1;}

	_bH=document.body.clientHeight;
	_top1=document.body.scrollTop+evn.clientY+6;
	_oH=o.offsetHeight;
	_top=((evn.clientY+_oH)>_bH)?(_top1-_oH-6):_top1;
	if((evn.clientY+_oH)>_bH){_top1=(_oH<evn.clientY)?(_top1-_oH-6):_top1;}
	o.style.left=_left;
	o.style.top=_top;
}

function getalt(i){
 o = $("_altlayer");
 if(i =='search'){
 o.innerHTML="<img src='../images/searchalt.gif' border='0' width='150' height='40' />";
 }else{
	 o.innerHTML="<img src='"+i+"' border='0' width='160' height='160' />";
 
 }
 
 o.style.display="block";
}
function quickalt(){
	o = $("_altlayer");
	if(o.style.display=="block"){
		Move(event,o);
	}
}
function restorealt(){
	$("_altlayer").style.display="none";
}







var req="";
var today = new Date();
function clearmsg() {
	var v1 = document.getElementById("allmsg");
	v1.value = "";
}

function creatRequest() {
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		
		try {
			XMLHttpReq = new ActiveXObject("Msxm12.XMLHTTP");
		} catch (e) {
			try {
				XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
	if (!req) {
		alert("ERROR");
	}

}

function reqPost() {
	var msg = document.getElementsByName("sendmsg")[0];
	if(msg.value!=""){
		var toId = document.getElementById("toUser").value;
		if(toId!=""){
			var sendid = document.getElementsByName("userid")[0].value;
			var par = document.createElement("");
			var chi = document.createTextNode("时间："+today.toLocaleString()+
					"\r"+"我说："+msg.value+"\r"
					+"----------------------"+"\r");
			par.appendChild(chi);
			
			var v2 = document.getElementById("allmsg");
			v2.appendChild(par);
			creatRequest();
			
			var m =sendid+"●☆▲"+toId+"●☆▲"+msg.value;
			var url = "SendMsgServlet";
	        
			req.open("post", url, true);
			req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

			req.send("msg="+m);
			
			msg.value="";
		}else{
			alert("请选择要聊天的对象...");
		}
		
	}else{
		alert("不能发送空消息...");
	}
}

window.onload = function getMeg(){
	creatRequest();
	var sendid = document.getElementsByName("userid")[0].value;
	var url = "ReceiveServlet?id="+sendid;
	
	req.open("get", url, true);
	
	req.onreadystatechange=callback;
	req.send(null);
	var v = setTimeout("getMeg()",1000);
}

function callback(){
	if(req.readyState==4){
		if(req.status==200){
			var rep = req.responseText;
			if(rep!=""){
				var par = document.createElement("");
				var chi = document.createTextNode(rep+"\r"+
						"******************"+"\r");
				par.appendChild(chi);

				var v2 = document.getElementById("allmsg");
				v2.appendChild(par);
			}
			
		}
	}
	
}